package club.haomoon.core.mapper.log;

import club.haomoon.core.cache.SpringUtils;
import club.haomoon.core.common.BaseDataMpper;
import club.haomoon.core.entity.log.UserAccountLogData;
import club.haomoon.core.entity.user.UserData;
import club.haomoon.core.enums.AccountLogEnum;
import club.haomoon.core.mapper.user.UserDataMapper;
import club.haomoon.core.util.StringUtils;
import club.haomoon.core.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.math.BigDecimal;


/**
 * -------------------------
 * @Description: 账户变更日志 Mapper
 * @Author: Wangjianwei
 * @Date: 2025/02/12 13:27
 * -------------------------
 */
public interface UserAccountLogDataMapper extends BaseDataMpper<UserAccountLogData> {

    /**
     * 增加
     *
     * @param userId 用户ID
     * @param changeType 变动类型
     * @param changeAmount 变动金额
     * @param sourceId 来源ID
     * @param sourceSn 来源订单
     * @param remark 备注信息
     * @param extra 预留字段
     */
    default void appendData(Integer userId, Integer changeType, BigDecimal changeAmount,
                     Integer sourceId, String sourceSn, String remark, String extra) {
            UserDataMapper userMapper = SpringUtils.getBean(UserDataMapper.class);
            UserData user = userMapper.selectById(userId);
            if (StringUtils.isNotNull(user)) {
                Integer changeObject = AccountLogEnum.getChangeObject(changeType);
                BigDecimal leftAmount = user.getUserMoney().add(changeAmount);
                UserAccountLogData logMoney = new UserAccountLogData();
                logMoney.setSn(this.randMakeOrderSn());
                logMoney.setUserId(userId);
                logMoney.setSourceSn(sourceSn);
                logMoney.setChangeObject(changeObject);
                logMoney.setChangeType(changeType);
                logMoney.setChangeAmount(changeAmount);
                logMoney.setLeftAmount(leftAmount);
                logMoney.setAction(1);
                logMoney.setRemark(remark);
                logMoney.setExtra(extra);
                logMoney.setCreateTime(System.currentTimeMillis() / 1000);
                logMoney.setUpdateTime(System.currentTimeMillis() / 1000);
                this.insert(logMoney);
            }
    }

    /**
     * 减少
     *
     * @param userId 用户ID
     * @param changeType 变动类型
     * @param changeAmount 变动金额
     * @param sourceId 来源ID
     * @param sourceSn 来源订单
     * @param remark 备注信息
     * @param extra 预留字段
     */
    default void subData(Integer userId, Integer changeType, BigDecimal changeAmount,
                     Integer sourceId, String sourceSn, String remark, String extra) {
            UserDataMapper userMapper = SpringUtils.getBean(UserDataMapper.class);
            UserData user = userMapper.selectById(userId);
            if (StringUtils.isNotNull(user)) {
                BigDecimal leftAmount = user.getUserMoney().subtract(changeAmount);

                UserAccountLogData logMoney = new UserAccountLogData();
                logMoney.setSn(this.randMakeOrderSn());
                logMoney.setUserId(userId);
                logMoney.setSourceSn(sourceSn);
                logMoney.setChangeType(changeType);
                logMoney.setChangeAmount(changeAmount);
                logMoney.setLeftAmount(leftAmount);
                logMoney.setAction(2);
                logMoney.setRemark(remark);
                logMoney.setExtra(extra);
                logMoney.setCreateTime(System.currentTimeMillis() / 1000);
                logMoney.setUpdateTime(System.currentTimeMillis() / 1000);
                this.insert(logMoney);
            }
    }

    /**
     * 生成唯一流水号
     * @return String
     */
    default String randMakeOrderSn() {
        String date = DateUtil.timestampToDate(System.currentTimeMillis()/1000, "yyyyMMddHHmmss");
        String sn;
        while (true) {
            sn = date + StringUtils.randomInt(12);
            UserAccountLogData snModel = this.selectOne(
                    new QueryWrapper<UserAccountLogData>()
                            .select("id")
                            .eq("sn", sn)
                            .last("limit 1"));
            if (snModel == null) {
                break;
            }
        }
        return sn;
    }
}
